package com.example.listening.examsdk;

import android.content.Context;
import android.util.Log;

import com.example.listening.Config;
import com.example.listening.examsdk.core.ConfigurablePathStrategy;
import com.example.listening.examsdk.core.ExamUtil;
import com.example.listening.examsdk.core.Lesson;
import com.example.listening.examsdk.core.LessonGroup;
import com.example.listening.examsdk.core.PathStrategy;
import com.example.listening.utils.AllContacts;
import org.json.JSONObject;
import java.io.File;
import java.lang.ref.SoftReference;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by tong on 15/11/18.
 */
public class ExamSDK {
    public static final String JSON_PREFIX = "json://";

    public static final String FREE_DIR_NAME = "free";
    private static final String TAG = ExamSDK.class.getSimpleName();
    public static boolean debug = true;

    private static PathStrategy pathStrategy;
    private static final Map<Object,SoftReference> cacheMap = new ConcurrentHashMap<Object,SoftReference>();
    private static ExamProperties properties;

    /**
     * 初始化
     * @param context
     */
    public static synchronized void init(Context context) {
        ExamProperties defaultProperties = new ExamProperties();
        defaultProperties.put(ExamProperties.KEY_ROOT_PATH, AllContacts.sdcard);

        defaultProperties.put(ExamProperties.KEY_SUBJECT_RELATIVE,"subject/${lessonNo}");

        defaultProperties.put(ExamProperties.KEY_DEFAULT_LESSION,"examInOne/" + Config.initJson + ".json");

        ExamSDK.properties = defaultProperties;

        d(properties);
        pathStrategy = new ConfigurablePathStrategy(properties);
    }

    /**
     * 初始化
     * @param context
     */
    public static synchronized void init(Context context,String filePath) {
        if (filePath != null && filePath.contains("freeExam")) {
            // /storage/emulated/0/WanXingTianKong/ExamResource/freeExam/S13_0_0000.json

            String splitString = "/ExamResource/";

            String path = filePath.split(splitString)[1];

            String subjectNum = path.replace(".json","");
            subjectNum = subjectNum.substring(subjectNum.length() - 1);

            ExamProperties defaultProperties = new ExamProperties();
            defaultProperties.put(ExamProperties.KEY_ROOT_PATH, AllContacts.sdcard);

            defaultProperties.put(ExamProperties.KEY_SUBJECT_RELATIVE,"subject/Free" + subjectNum);

            defaultProperties.put(ExamProperties.KEY_DEFAULT_LESSION,path);

            ExamSDK.properties = defaultProperties;

            d(properties);
            pathStrategy = new ConfigurablePathStrategy(properties);
        }
        else {
            init(context);
        }
    }

    /**
     * 获取指定位置的套题
     * @param location
     * @return
     */
    public static LessonGroup getLessionGroup(String location) {
        d("getLessionGroup: " + location);
        File file = new File(pathStrategy.getRootPath(), location);
        JSONObject obj = ExamUtil.getJSONObject(file);
        return new LessonGroup(location, obj);
    }

    /**
     * 获取默认课程组信息
     * @return
     */
    public static LessonGroup getDefaultLessonGroup() {
        return getLessionGroup(properties.getProperty(ExamProperties.KEY_DEFAULT_LESSION));
    }

    /**
     * 获取课程
     * @param lessonId
     * @return
     */
    public static Lesson getLession(String lessonId) {
        d("getLession: " + lessonId);
        LessonGroup lessonGroup = getDefaultLessonGroup();
        for (Lesson lesson : lessonGroup.getLessons()) {
            if (lesson.getId().equals(lessonId)) {
                return lesson;
            }
        }
        return null;
    }

    public static PathStrategy getPathStrategy() {
        return pathStrategy;
    }

    public static void d(String tag,Object obj) {
        if (debug) {
            String content = "";
            if (obj instanceof String) {
                content = (String)obj;
            } else if (obj != null) {
                content = obj.toString();
            }
            Log.d(tag,content);
        }
    }

    public static void d(Object obj) {
        ExamSDK.d(">>" + TAG + "<<",obj);
    }
}
